#!/bin/python

# --------------------------------------
# VIDEO RENAMER - regex tests
# --------------------------------------
# Ranames files based on a specific regex pattern in 
#  show_list.db and based on a specific list of file 
#  extensions. This file is not needed for VB to run.
# --------------------------------------

import sys
import re

filenames = ["top_gear.14x02.hdtv_xvid-fov.avi", 
	"top_gear.s14e02.hdtv_xvid-fov.avi", 
	"top_gear.S14E02.hdtv_xvid-fov.avi",
	"top_gear.1402.hdtv_xvid-fiv.avi",
	"top_gear.302.hdtv_xvid-fiv.avi",]

expressions = ["(\d\d)x(\d\d)", "s(\d\d)e(\d\d)", "(\d\d\d\d)|(\d\d\d)"]

for filename in filenames:
	print("\nMatching: " + filename)
	# Run through all the expressions
	for expression in expressions:
		print(expression)
		found = re.search(expression, filename)
		if found is not None:
			seasEp = filename[found.start():found.end()]
			
			# Redo season episode format to SXXEXX
			if expression == expressions[0]:
				tempSeasEp = "S" + seasEp[0:2] + "E" + seasEp[3:5]
				seasEp = tempSeasEp
				
			if expression == expressions[1]:
				tempSeasEp = "S" + seasEp[1:3] + "E" + seasEp[4:6]
				seasEp = tempSeasEp
				
			if expression == expressions[2]:
				if len(seasEp) == 3:
					tempSeasEp = "S0" + seasEp[0:1] + "E" + seasEp[1:3]
					seasEp = tempSeasEp
				
				if len(seasEp) == 4:
					tempSeasEp = "S" + seasEp[0:2] + "E" + seasEp[2:4]
					seasEp = tempSeasEp
			
			showName = filename[:found.start()]
			# Final filename
			rename = showName + seasEp
			print("\t" + rename)